﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QASampling.BusinessObject;
using QASampling.UI;

namespace QASampling.QAControl
{
    public partial class QADataGridWithButton : UserControl
    {
        //added by Vincent 20130206 for handling multiple selected rows
        private int rowCountSelected = 0;
        //-------------------------------------------------------------

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private string tableSource;
        public string TableSource
        {
            get { return tableSource; }
            set
            {
                tableSource = value;
                NotifyPropertyChanged("TableSource");
            }
        }

        private string[] fieldName;
        public string[] FieldName
        {
            get { return fieldName; }
            set
            {
                fieldName = value;
                NotifyPropertyChanged("FieldName");
            }
        }

        private string[] labelText;
        public string[] LabelText
        {
            get { return labelText; }
            set
            {
                labelText = value;
                NotifyPropertyChanged("LabelText");
            }
        }

        private int[] fieldLength;
        public int[] FieldLength
        {
            get { return fieldLength; }
            set
            {
                fieldLength = value;
                NotifyPropertyChanged("FieldLength");
            }
        }

        private JenisField[] fieldType;
        public JenisField[] FieldType
        {
            get { return fieldType; }
            set
            {
                fieldType = value;
                NotifyPropertyChanged("FieldType");
            }
        }

        private string fieldRules;
        public string FieldRules
        {
            get { return fieldRules; }
            set
            {
                fieldRules = value;
                NotifyPropertyChanged("FieldRules");
            }
        }

        private string[] dateFormat;
        public string[] DateFormat
        {
            get { return dateFormat; }
            set
            {
                dateFormat = value;
                NotifyPropertyChanged("DateFormat");
            }
        }

        private string gridLabel;
        public string GridLabel
        {
            get { return gridLabel; }
            set { gridLabel = value; }
        }

        private string tableNo;
        public string TableNo
        {
            get { return tableNo; }
            set
            {
                tableNo = value;
                NotifyPropertyChanged("TableNo");
            }
        }

        private int[] digits;
        public int[] Digits
        {
            get { return digits; }
            set 
            { 
                digits = value;
                NotifyPropertyChanged("Digits");
            }
        }

        private string[] fieldValue;
        private string[] originalValue;
        private string[] deleteValue;

        internal QADataGrid Datagrid
        {
            get { return qaDataGrid1; }
        }

        public QADataGridWithButton()
        {
            InitializeComponent();
            btnInsert.Visible = !QAController.ReadOnly;
            btnDelete.Visible = !QAController.ReadOnly;
            lblInsert.Visible = !QAController.ReadOnly;
            lblDelete.Visible = !QAController.ReadOnly;
            this.PropertyChanged += OnPropertyChanged;
        }

        void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "TableSource")
            {
                qaDataGrid1.TableSource = tableSource;
                btnInsert.TableSource = tableSource;
                btnDelete.TableSource = tableSource;
                btnEdit.TableSource = tableSource;
            }
            else if (e.PropertyName == "FieldName")
            {
                if (fieldName != null)
                {
                    if (!fieldName.Contains<string>(QAConstants.MUST_HAVE_FIELD))
                    {
                        string[] ar2 = new string[fieldName.Length + 1];
                        fieldName.CopyTo(ar2, 0);
                        ar2.SetValue(QAConstants.MUST_HAVE_FIELD, fieldName.Length);
                        fieldName = ar2;
                    }

                    if (dateFormat != null)
                    {
                        if (dateFormat.Length < fieldName.Length)
                        {
                            string[] ar2 = new string[fieldName.Length];
                            dateFormat.CopyTo(ar2, 0);
                            dateFormat = ar2;
                        }
                    }
                    else
                        dateFormat = new string[fieldName.Length];

                    //qaDataGrid1.DateFormat = dateFormat;

                    if (fieldType != null)
                    {
                        if (fieldType.Length < fieldName.Length)
                        {
                            JenisField[] ar2 = new JenisField[fieldName.Length];
                            fieldType.CopyTo(ar2, 0);
                            fieldType = ar2;
                        }
                    }
                    else
                    {
                        fieldType = new JenisField[fieldName.Length];

                        for (int i = 0; i < fieldType.Length; i++)
                        {
                            fieldType[i] = JenisField.Varchar;
                        }
                    }

                    //qaDataGrid1.FieldType = fieldType;

                    if (fieldLength != null)
                    {
                        if (fieldLength.Length < fieldName.Length)
                        {
                            int[] ar2 = new int[fieldName.Length];
                            fieldLength.CopyTo(ar2, 0);
                            fieldLength = ar2;
                        }
                    }
                    else
                        fieldLength = new int[fieldName.Length];

                    qaDataGrid1.FieldName = fieldName;
                    btnInsert.FieldName = fieldName;
                    btnEdit.FieldName = fieldName;
                }
            }
            else if (e.PropertyName == "LabelText")
            {
                if (labelText != null)
                {
                    if (!labelText.Contains<string>(QAConstants.MUST_HAVE_FIELD))
                    {
                        string[] ar2 = new string[labelText.Length + 1];
                        labelText.CopyTo(ar2, 0);
                        ar2.SetValue(QAConstants.MUST_HAVE_FIELD, labelText.Length);
                        labelText = ar2;
                    }

                    btnInsert.LabelText = labelText;
                    qaDataGrid1.LabelText = labelText;
                    btnEdit.LabelText = labelText;
                }
            }
            else if (e.PropertyName == "FieldLength")
            {
                btnInsert.FieldLength = fieldLength;
                btnEdit.FieldLength = fieldLength;
            }
            else if (e.PropertyName == "FieldRules")
            {
                qaDataGrid1.FieldRules = fieldRules;
            }
            else if (e.PropertyName == "DateFormat")
            {
                btnInsert.DateFormat = dateFormat;
                btnEdit.DateFormat = dateFormat;
                qaDataGrid1.DateFormat = dateFormat;
            }
            else if (e.PropertyName == "FieldType")
            {
                btnInsert.FieldType = fieldType;
                btnEdit.FieldType = fieldType;
                qaDataGrid1.FieldType = fieldType;
            }
            else if (e.PropertyName == "TableNo")
            {
                qaDataGrid1.TableNo = tableNo;
                btnInsert.TableNo = tableNo;
            }
            else if (e.PropertyName == "Digits")
            {
                qaDataGrid1.Digits = digits;
                btnInsert.Digits = digits;
            }
        }

        private void qaDataGrid1_SelectionChanged(object sender, EventArgs e)
        {
            if (qaDataGrid1.SelectedRows.Count == 0)
                return;
            if (qaDataGrid1.Columns.Contains(QAConstants.MUST_HAVE_FIELD))
            {
                List<string> lstValue = new List<string>();
                for (int i = 0; i < fieldName.Length; i++)
                {
                    lstValue.Add(qaDataGrid1.SelectedRows[0].Cells[fieldName[i]].FormattedValue.ToString());
                }
                originalValue = lstValue.ToArray();
                fieldValue = lstValue.ToArray();
                deleteValue = new string[] { qaDataGrid1.SelectedRows[0].Cells[QAConstants.MUST_HAVE_FIELD].Value.ToString() };
                /* btnDelete.OriginalValue = lstValue.ToArray();
                 btnDelete.FieldValue = new string[] { qaDataGrid1.SelectedRows[0].Cells[QAConstants.MUST_HAVE_FIELD].Value.ToString() };

                 btnEdit.OriginalValue = lstValue.ToArray();
                 btnEdit.FieldValue = lstValue.ToArray();*/
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (fieldName.Length == 0)
            {
                throw new Exception(this.Name + " FieldName belum diisi");
            }
            frmQATable f = new frmQATable();
            try
            {
                f.FormClosed += new FormClosedEventHandler(f_FormClosed);
                f.FieldName = fieldName;
                f.TableSource = tableSource;
                f.FieldCommand = JenisCommand.Insert;
                f.LabelText = labelText;
                f.FieldLength = fieldLength;
                f.FieldType = fieldType;
                f.DateFormat = dateFormat;
                f.Text = JenisCommand.Insert.ToString();
                f.TableNo = tableNo;
                f.Digits = digits;
                f.Show(this);
            }
            catch (Exception)
            {
                throw new Exception("Field tidak ditemukan");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //if and else added by Vincent 20130206 for handling multiple row selected
            if (rowCountSelected>1)
            {
                MessageBox.Show("Harap pilih hanya satu data yang akan dihapus");
            }
            else
            {
                if (fieldName.Length == 0)
                {
                    throw new Exception(this.Name + " FieldName belum diisi");
                }

                frmQATable f = new frmQATable();
                try
                {
                    f.FormClosed += new FormClosedEventHandler(f_FormClosed);
                    f.FieldName = new string[] { "ID" };
                    f.FieldValue = deleteValue;
                    f.TableSource = tableSource;
                    f.FieldCommand = JenisCommand.Delete;
                    f.LabelText = labelText;
                    f.FieldLength = fieldLength;
                    f.FieldType = fieldType;
                    f.DateFormat = dateFormat;
                    f.Text = JenisCommand.Delete.ToString();
                    f.FieldBefore = originalValue;
                    f.TableNo = tableNo;
                    f.Show(this);
                }
                catch (Exception)
                {
                    throw new Exception("Field tidak ditemukan");
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //if and else added by Vincent 20130206 for handling multiple row selected
            if (rowCountSelected > 1)
            {
                MessageBox.Show("Harap pilih hanya satu data yang akan diubah");
            }
            else
            {
                if (fieldName.Length == 0)
                {
                    throw new Exception(this.Name + " FieldName belum diisi");
                }

                frmQATable f = new frmQATable();

                try
                {       
                    f.FormClosed += new FormClosedEventHandler(f_FormClosed);
                    f.FieldName = fieldName;
                    f.FieldValue = fieldValue;
                    f.TableSource = tableSource;
                    f.FieldCommand = JenisCommand.Update;
                    f.LabelText = labelText;
                    f.FieldLength = fieldLength;
                    f.FieldType = fieldType;
                    f.DateFormat = dateFormat;
                    f.Text = JenisCommand.Update.ToString();
                    f.FieldBefore = originalValue;
                    f.TableNo = tableNo;

                    //added by Vincent 20130206 for fixing error when button update clicked
                    f.Digits = digits;
                    //---------------------------------------------------------------------
                    
                    f.Show(this);
                }
                catch (Exception)
                {
                    throw new Exception("Field tidak ditemukan");
                }
            }
        }

        void f_FormClosed(object sender, FormClosedEventArgs e)
        {
            qaDataGrid1.TableSource = tableSource;

            //added by Vincent 20130225 for deselect row
            qaDataGrid1.ClearSelection();
            rowCountSelected = 0;
            //------------------------------------------
        }

        private void qaDataGrid1_Click(object sender, EventArgs e)
        {
            //added by Vincent 20130206 for handling multiple selected rows
            rowCountSelected = qaDataGrid1.SelectedRows.Count;
        }
    }
}
